BluetoothPeripheral PRO
The BluetoothPeripheral interface represents a Bluetooth Low Energy (BLE) peripheral device. It provides properties and methods for interacting with the device, including connecting, discovering services and characteristics, reading/writing values, and subscribing to notifications.
Properties (Read-only)
id: string
A unique identifier (UUID string) for the peripheral. It remains constant across app launches and can be used to identify and reconnect to the device.
name: string | null
The name of the peripheral device, or null if not available (e.g., if not broadcast).
isConnected: boolean
Indicates whether the peripheral is currently connected.
true: Connected and ready for communicationfalse: Not connected or disconnected
canSendWriteWithoutResponse: boolean
Indicates whether the peripheral is ready to send write requests without waiting for a response.
true: Supports fast, unacknowledged writes (writeWithoutResponse)false: Requires response before next write
The onReadyToSendWriteWithoutResponse callback is triggered when this changes to true.
ancsAuthorized: boolean
Indicates whether the peripheral is authorized to use Apple Notification Center Service (ANCS). Relevant only for peripherals that support ANCS.
services: BluetoothService[] | null
The list of services discovered on the peripheral. null if discoverServices() has not been called yet.
Event Handlers
onConnected: (() => void) | null
Called when the peripheral is successfully connected.
onDisconnected: ((error: Error | null, isReconnecting: boolean) => void) | null
Called when the peripheral is disconnected.
error: An error object if the disconnection was unexpected, ornullif intentionalisReconnecting:trueif the system is attempting to reconnect automatically
onConnectFailed: ((error: Error) => void) | null
Called when the peripheral fails to connect.
onNameChanged: ((name: string | null) => void) | null
Called when the peripheral's name changes.
onDiscoverServices: ((error: Error | null, services: BluetoothService[] | null) => void) | null
Called after calling discoverServices().
onDiscoverCharacteristics: ((error: Error | null, characteristics: BluetoothCharacteristic[] | null) => void) | null
Called after calling discoverCharacteristics().
onDiscoverIncludedServices: ((error: Error | null, services: BluetoothService[] | null) => void) | null
Called after calling discoverIncludedServices().
onReadyToSendWriteWithoutResponse: (() => void) | null
Called when the peripheral is ready to send more write requests without response.
Methods
readValue(characteristic: BluetoothCharacteristic): Promise<Data>
Reads the value of a given characteristic.
-
Parameters:
characteristic: The characteristic to read from
-
Returns: A
Promise<Data>containing the value
maxWriteValueLength(writeType: "withResponse" | "withoutResponse"): number
Gets the maximum number of bytes that can be written in a single operation.
-
Parameters:
writeType:"withResponse"or"withoutResponse"
-
Returns: A
numberrepresenting the max write size
writeValue(characteristic, value, writeType): Promise<void>
Writes data to a characteristic.
-
Parameters:
characteristic: Target characteristicvalue: The data to write (Data)writeType:"withResponse"or"withoutResponse"
-
Returns:
Promise<void>
subscribe(characteristic, handler): Promise<void>
Subscribes to notifications or indications from a characteristic.
-
Requirements: The characteristic must support
"notify"or"indicate" -
Parameters:
-
characteristic: Target characteristic -
handler(error, value): Callback triggered on value changeerror: Error object if anyvalue: New value asData
-
-
Returns:
Promise<void>
unsubscribe(characteristic): Promise<void>
Unsubscribes from notifications for a characteristic.
-
Parameters:
characteristic: Target characteristic
-
Returns:
Promise<void>
discoverServices(serviceUUIDs?: string[]): Promise<void>
Discovers the services available on the peripheral.
-
Parameters:
serviceUUIDs: Optional list of service UUIDs to filter
-
Returns:
Promise<void>
discoverIncludedServices(service, includedServiceUUIDs?): Promise<void>
Discovers included services within a service.
-
Parameters:
service: The parent serviceincludedServiceUUIDs: Optional list of included service UUIDs
-
Returns:
Promise<void>
discoverCharacteristics(service, characteristicUUIDs?): Promise<void>
Discovers characteristics within a service.
-
Parameters:
service: The target servicecharacteristicUUIDs: Optional list of characteristic UUIDs
-
Returns:
Promise<void>
readRSSI(): Promise<number>
Reads the current Received Signal Strength Indicator (RSSI).
- Returns: A
Promise<number>representing signal strength in dBm
Examples
Connect and Read Characteristics
Write Value and Subscribe
Notes
- Always call
discoverServices()anddiscoverCharacteristics()before interacting with a peripheral’s data. - Be sure to
unsubscribe()when notifications are no longer needed. - Avoid writing when
canSendWriteWithoutResponseisfalse.
